﻿@{
    Layout = "~/Views/Shared/_LayoutAdminPage.cshtml";
}

@section header{

    <style>
        #articleCateTable + .layui-table-view .layui-table-body tbody > tr > td {
            padding: 0;
        }

            #articleCateTable + .layui-table-view .layui-table-body tbody > tr > td > .layui-table-cell {
                height: 48px;
                line-height: 48px;
            }

        .tb-img-circle {
            width: 90px;
            height: 40px;
            cursor: zoom-in;
            /* border-radius: 50%;
            border: 2px solid #dddddd;*/
        }


        .articleCate-info-img {
            width: 100%;
            height: 110px;
            line-height: 110px;
            /* position: relative; */
            /* display: inline-block; */
            /* border: 2px solid #eee; */
            /*border-radius: 50%;*/
            overflow: hidden;
            cursor: pointer;
            margin: 0 auto;
        }

            .articleCate-info-img:hover:after {
                content: '\e681';
                position: absolute;
                text-align: center;
                top: 0;
                left: 0px;
                right: 0;
                bottom: 0;
                color: #fff;
                background-color: rgba(0, 0, 0, 0.3);
                font-size: 28px;
                padding-top: 2px;
                font-style: normal;
                font-family: layui-icon;
                -webkit-font-smoothing: antialiased;
                -moz-osx-font-smoothing: grayscale;
            }

            .articleCate-info-img img {
                width: 100%;
                height: 110px;
            }
    </style>

}

<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 表格工具栏 -->
            <form class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label">名&emsp;称:</label>
                        <div class="layui-input-inline">
                            <input name="name" class="layui-input" placeholder="输入名称" />
                        </div>
                    </div>

                    <div class="layui-inline">
                        &emsp;
                        <button class="layui-btn icon-btn" lay-filter="articleCateTbSearch" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>
                    </div>
                </div>
            </form>
            <!-- 数据表格 -->
            <table id="articleCateTable" lay-filter="articleCateTable"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="articleCateTbBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>


<!-- 表单弹窗 -->
<script type="text/html" id="articleCateEditDialog">
    <form id="articleCateEditForm" lay-filter="articleCateEditForm" class="layui-form model-form">
        <input name="id" value="0" type="hidden" />
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">名称:</label>
            <div class="layui-input-block">
                <input name="name" placeholder="请输入名称" class="layui-input"
                       lay-verType="tips" lay-verify="required" required />
            </div>
        </div>


        <div class="layui-form-item" id="editArticleCateImage">
            <label class="layui-form-label">图片上传:</label>
            <div class="layui-input-block">
                <div class="articleCate-info-img" id="articleCateInfoImg">
                    <img src="" alt="" />
                    <input name="image" type="hidden" class="layui-input" />
                </div>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">SEO关键词:</label>
            <div class="layui-input-block">
                <input name="keywords" placeholder="请输入SEO关键词" class="layui-input" />
                <div class="layui-word-aux">不填写则按SEO默认方案配置</div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">SEO描述:</label>
            <div class="layui-input-block">
                <input name="description" placeholder="请输入SEO描述" class="layui-input" />
                <div class="layui-word-aux">不填写则按SEO默认方案配置</div>
            </div>
        </div>



        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">排序:</label>
            <div class="layui-input-block">
                <input name="sort" placeholder="请输入排序号" value="1" type="number" class="layui-input"
                       lay-verType="tips" lay-verify="required|number" required />
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">状态:</label>
            <div class="layui-input-block">
                <input type="radio" name="status" value="1" title="正常" checked />
                <input type="radio" name="status" value="0" title="禁用" />
            </div>
        </div>


        <div class="layui-form-item text-right">
            <button class="layui-btn" lay-filter="articleCateEditSubmit" lay-submit>保存</button>
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        </div>
    </form>
</script>


<script>
    layui.use(['layer', 'form', 'table', 'util', 'admin', 'xmSelect', 'notice', 'fileChoose'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var admin = layui.admin;
        var xmSelect = layui.xmSelect;
        var notice = layui.notice;
        var fileChoose = layui.fileChoose;


        /* 渲染表格 */
        var insTb = table.render({
            elem: '#articleCateTable',
            url: '/api/admin/ArticleCate/GetArticleCateList',
            //limit: 20, //默认展示数
            page: true,
            toolbar: ['<p>',
                '<button lay-event="add" class="layui-btn layui-btn-sm icon-btn"><i class="layui-icon">&#xe654;</i>添加</button>&nbsp;',
                '<button lay-event="del" class="layui-btn layui-btn-sm layui-btn-danger icon-btn"><i class="layui-icon">&#xe640;</i>删除</button>',
                '</p>'].join(''),
            cellMinWidth: 100,
            cols: [[
                { type: 'checkbox' },
                { field: 'id', width: 60, align: 'center', sort: true },
                { field: 'name', title: '名称', align: 'center' },
                { field: 'keywords', title: '关键词' },
                { field: 'description', title: '描述' },
                { field: 'count', title: '文章数量' },
                { field: 'sort', title: '排序', sort: true },
                {
                    field: 'status', title: '状态', templet: function (d) {
                        var strs = [
                            '<span class="text-danger">禁用</span>',
                            '<span class="text-success">正常</span>'
                        ];
                        return strs[d.status];
                    }, align: 'center', sort: true
                },
                {
                    field: 'createTime', title: '创建时间', templet: function (d) {
                        return util.toDateString(d.createTime);
                    }, sort: true
                },
                { title: '操作', toolbar: '#articleCateTbBar', align: 'center', minWidth: 200 }
            ]]
        });


        /* 表格搜索 */
        form.on('submit(articleCateTbSearch)', function (data) {
            insTb.reload({ where: data.field, page: { curr: 1 } });
            return false;
        });

        /* 表格工具条点击事件 */
        table.on('tool(articleCateTable)', function (obj) {
            if (obj.event === 'edit') { // 修改
                showEditModel(obj.data);
            } else if (obj.event === 'del') { // 删除
                doDel(obj);
            }
        });

        /* 表格头工具栏点击事件 */
        table.on('toolbar(articleCateTable)', function (obj) {
            if (obj.event === 'add') { // 添加
                showEditModel();
            } else if (obj.event === 'del') { // 删除
                var checkRows = table.checkStatus('articleCateTable');
                if (checkRows.data.length === 0) {
                    layer.msg('请选择要删除的数据', { icon: 2 });
                    return;
                }
                var ids = checkRows.data.map(function (d) {
                    return d.id;
                });
                doDel({ ids: ids });
            }
        });

        /* 显示表单弹窗 */
        function showEditModel(mData) {
            admin.open({
                type: 1,
                title: (mData ? '修改' : '添加') + '信息',
                content: $('#articleCateEditDialog').html(),
                success: function (layero, dIndex) {
                    // 回显表单数据
                    form.val('articleCateEditForm', mData);

                    if (mData) {
                        $("#articleCateInfoImg >img").attr("src", mData.image);
                    }

                    /* 选择图片 */
                    $('#articleCateInfoImg').click(function () {
                        fileChoose.open({
                            fileUrl: '',
                            listUrl: '/api/admin/File/getfile',
                            num: 3,
                            dialog: {
                                offset: '60px'
                            },
                            upload: {
                                data: {
                                    dir: function () {
                                        return $('#tv-current-position').text();
                                    }
                                },
                                url: "/api/admin/Public/Upload/UploadFile2"
                            },
                            onChoose: function (urls) {
                                $('#articleCateInfoImg > img').attr('src', urls[0].url);
                                $('#articleCateInfoImg > input').val(urls[0].url);
                                //layer.msg('你选择了：' + JSON.stringify(urls), { icon: 1 });
                            }
                        });


                    });


                    // 表单提交事件
                    form.on('submit(articleCateEditSubmit)', function (data) {
                        var loadIndex = layer.load(2);
                        $.post(mData ? '/api/admin/ArticleCate/EditArticleCateInfo' : '/api/admin/ArticleCate/AddArticleCateInfo', data.field, function (res2) {
                            layer.close(loadIndex);
                            if (1 == res2.code) {
                                layer.close(dIndex);
                                layer.msg(res2.message, { icon: 1 });
                                insTb.reload({ page: { curr: 1 } });
                            } else {
                                layer.msg(res2.message, { icon: 2 });
                            }
                        }, 'json');
                        return false;
                    });
                }
            });
        }

        /* 删除 */
        function doDel(obj) {
            layer.confirm('确定要删除选中栏目数据吗！？(会将栏目下的文章全部删除！)', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                var loadIndex = layer.load(2);
                $.post('/api/admin/ArticleCate/DeleteArticleCate', {
                    ids: obj.data ? obj.data.id : obj.ids.join(',')
                }, function (res) {
                    layer.close(loadIndex);
                    if (res.code == 1) {
                        layer.msg(res.message, { icon: 1 });
                        insTb.reload({ page: { curr: 1 } });
                    } else {
                        layer.msg(res.message, { icon: 2 });
                    }
                }, 'json');
            });
        }

    });

</script>